package org.crmsystem.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.model.*;
import org.crmsystem.service.IDeptService;
import org.crmsystem.service.IJobService;
import org.crmsystem.service.IStaffService;
import org.crmsystem.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/11.
 */
@Controller
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private IStaffService iStaffService;
    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IJobService iJobService;
    private static Logger logger= LogManager.getLogger(StaffController.class);
    @RequestMapping("/getAllStaff")
    public String getAllStaff(){
        return "staffList";
    }
    @ResponseBody
    @RequestMapping(value = "/staffList",method = RequestMethod.POST)
    public Result<?> getAllStaffList(Integer currPage, Integer pageSize,String startDate,String endDate,String select,String condition){
        Staff staff=new Staff();
        if("staffSex".equals(select)){
            staff.setSex(condition);
        }else if("staffName".equals(select)){
            staff.setName(condition);
        }else if ("deptName".equals(select)){
            staff.setDeptName(condition);
        }else if("staffTel".equals(select)){
            staff.setTel(condition);
        }else if ("staffState".equals(select)){
            staff.setState(condition);
        }else if ("jobName".equals(select)){
            staff.setJobName(condition);
        }
        Map map=new HashMap();
        map.put("staff",staff);
        map.put("currPage",currPage);
        map.put("pageSize",pageSize);
        String sDate;
        if (startDate==null||startDate==""){
            sDate="1900-01-01";
        }else {
            sDate=startDate;
        }
        map.put("startDate",sDate);
        String eDate;
        if(endDate==null||endDate==""){
            eDate=DateUtil.formatDate(new Date(),"yyyy-MM-dd");
        }else {
            eDate=endDate;
        }
        map.put("endDate",eDate);
        Page page=iStaffService.getStaff(map);
        Map map1=new HashMap();
        map1.put("pageSize",pageSize);
        map1.put("currPage",currPage);
        map1.put("result",page.getResult());
        map1.put("totalPage",page.getTotalPage());
        map1.put("totalNum",page.getTotalNum());
        Result<Map<String,Object>> rl=new Result<>();
        rl.setData(map1);
        return rl;
    }
    @RequestMapping("/getInfo")
    public ModelAndView getInfo(){
        Dept dept=new Dept();
        Job job=new Job();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        Map map1=new HashMap();
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("addStaff",map1);
    }
    @ResponseBody
    @RequestMapping(value = "/addStaff",method = {RequestMethod.POST})
    public Result<?> addStaff(int deptId,String staffName,String staffJob,String duty,String sex,
                           String tel,String pho,String email,int jobId,String state,String leaveState,
                           String remark,String hireDate) throws ParseException{
        Staff staff=new Staff();
        staff.setRemark(remark);
        staff.setJobId(jobId);
        staff.setState(state);
        staff.setTel(tel);
        staff.setPho(pho);
        staff.setLeaveOffice(leaveState);
        logger.info(email);
        Date date;
        if(hireDate==null||hireDate==""){
            date=new Date();
        }else {
            date = DateUtil.formatDate(hireDate, "yyyy-MM-dd");
        }
        staff.setHireDate(date);
        staff.setDeptId(deptId);
        staff.setEmail(email);
        staff.setName(staffName);
        staff.setSex(sex);
        staff.setJob(staffJob);
        staff.setDuty(duty);
        int flag=iStaffService.addStaff(staff);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @RequestMapping("/toEditStaff")
    public ModelAndView toEditStaff(){
        Dept dept=new Dept();
        Job job=new Job();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        Map map1=new HashMap();
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("editStaff",map1);
    }
    @RequestMapping("/toStaffIntroduce")
    public String toStaffIntroduce(){
        return "staffIntroduce";
    }
    @RequestMapping(value = "/getStaff",method={RequestMethod.GET})
    public ModelAndView getStaff(int id,String staffName){
        logger.info(id);
        Staff staff=new Staff();
        staff.setId(id);
        staff.setName(staffName);
        Map map=new HashMap();
        map.put("currPage",0);
        map.put("pageSize",100);
        map.put("staff",staff);
        Page page=iStaffService.getStaff(map);
        List<Object> list=page.getResult();
        logger.info(list.size());
        Staff staff1=(Staff) list.get(0);
        Dept dept=new Dept();
        Job job=new Job();
        List<Dept> deptList=iDeptService.getDept(dept);
        List<Job> jobList=iJobService.getJob(job);
        Map map1=new HashMap();
        map1.put("staff",staff1);
        map1.put("deptList",deptList);
        map1.put("jobList",jobList);
        return new ModelAndView("editStaff",map1);
    }
    @ResponseBody
    @RequestMapping(value = "/editStaff",method = {RequestMethod.POST})
    public Result<?> editStaff(int staffId,int deptId,String staffName,String staffJob,String duty,String sex,
                           String tel,String pho,String email,int jobId,String state,String leaveState,
                           String remark,String hireDate) throws ParseException{
        Staff staff=new Staff();
        staff.setId(staffId);
        staff.setRemark(remark);
        staff.setJobId(jobId);
        logger.info(jobId);
        staff.setState(state);
        staff.setTel(tel);
        staff.setPho(pho);
        staff.setLeaveOffice(leaveState);
        Date date;
        if(hireDate==null||hireDate==""){
            date=new Date();
        }else {
            date = DateUtil.formatDate(hireDate, "yyyy-MM-dd");
        }
        staff.setHireDate(date);
        staff.setDeptId(deptId);
        staff.setEmail(email);
        staff.setName(staffName);
        staff.setSex(sex);
        staff.setJob(staffJob);
        staff.setDuty(duty);
        int flag=iStaffService.updateStaff(staff);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @RequestMapping(value = "/staffIntroduce",method = RequestMethod.GET)
    public ModelAndView showStaffInfo(int id,String staffName){
        Staff staff=new Staff();
        staff.setId(id);
        staff.setName(staffName);
        Map map=new HashMap();
        map.put("staff",staff);
        map.put("currPage",0);
        map.put("pageSize",100);
        Page page=iStaffService.getStaff(map);
        List<Object> list=page.getResult();
        Staff staff1=(Staff)list.get(0);
        Map map1=new HashMap();
        map1.put("staff",staff1);
        return new ModelAndView("staffIntroduce",map1);
    }
    @ResponseBody
    @RequestMapping(value ="/delStaff",method = RequestMethod.POST)
    public Result<?> delStaff(int id){
        int flag=iStaffService.delStaff(id);
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
    @ResponseBody
    @RequestMapping(value ="/delAllStaff",method = RequestMethod.POST)
    public Result<?> delAllStaff(String staffId){
        String staff[]=staffId.split(",");
        int flag=1;
        for(int i=0;i<staff.length;i++){
            if (staff[i]!=null){
                int id=Integer.parseInt(staff[i]);
                int influence=iStaffService.delStaff(id);
                if (influence<1){
                    flag=0;
                }
            }
        }
        Result<Integer> ri=new Result<>();
        ri.setData(flag);
        return ri;
    }
}
